<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/


/**
*	standard  GtkCalendar
*	
*	@param	
*	@return	
*	
*/
class calendar extends control {


	function __construct($glade,$name) {
		parent::__construct($glade,$name);

		// Some signal handlers
		$this->widget->connect('day-selected',array($this,'day_selected'));
		
	}

	/**
	*	Get a date as unixtimestamp
	*	
	*	@param	
	*	@return	
	*	
	*/
	function get_date() {
		$date = $this->widget->get_date();
		$result = mktime(0,0,0,$date[1]+1,$date[2],$date[0]);
		return($result);
	}

	/**
	*	Put a visual marker on a selected day
	*	
	*	@param	
	*	@return	
	*	
	*/
	function set_day($d) {

		$this->widget->select_day($d);
		$this->widget->clear_marks();
		$this->widget->mark_day($d);

	}


	/**
	*	A day has been clicked
	*	
	*	@param	
	*	@return	
	*	
	*/
	function day_selected() {

		$date = $this->widget->get_date();
		$this->widget->clear_marks();
		$this->widget->mark_day($date[2]);     // Set a marker on the selected day

	}




}


class checkbox extends control {

	function __construct($glade,$name,$font=NULL) {
		parent::__construct($glade,$name,$font);
		$this->widget->set_label(settings::xlate($this->widget->get_label()));
	}

	function set_text($label) {
		$this->widget->set_label(settings::xlate($label));
	}

	function set_checked($v=TRUE) {
		return( $this->set_active($v) );
	}

	function set_active($v=TRUE) {
		return( $this->widget->set_active($v) );
	}


	function is_checked() {
		return( $this->get_active() );
	}

	function get_active() {
		return( $this->widget->get_active() );
	}

}


class combo extends control {

	public $dat=array();				// Data to build combo
	public $combo=array();				// combo data array

	public $value_ctrl=FALSE;			// id of editarea control where dafault vlaue placed
	public $description_ctrl=FALSE;		// id of label desctiption control

	var $model = FALSE;

	function __construct($glade,$name,$font=NULL) {
		parent::__construct($glade,$name,$font);


		// Create a model
		if (defined("GObject::TYPE_STRING")) {
		    $this->model = new GtkListStore(GObject::TYPE_STRING);
		} else {
		    $this->model = new GtkListStore(GObject::TYPE_STRING);
		}
		$this->widget->set_model($this->model);

		$cellRenderer = new GtkCellRendererText(); // note 2
		$this->widget->pack_start($cellRenderer);
		$this->widget->set_attributes($cellRenderer, 'text', 0); // note 3 

	}

	function clear() {
		$this->model->clear();
	}

	function set_combo($dat=array()) {

		$this->dat = $dat;
		$this->combo = array();
		foreach( $this->dat as $val => $d ) {
			$this->combo[] = $d['label'];		
		}
		
	}

	function set_selected($code) {

		// Find the combo index for this code
		foreach ( $this->dat as $idx => $dat ) {
			if ( $code == $dat['code'] ) {
				$this->widget->set_active($idx);
			}
		}
	
	}


	function code() {

//print_r($this->dat);
//echo "---- selected :".$this->selected()."\n";

		$value = $this->dat[$this->selected()]['code'];

		return( $value );
	}

	
	// $tags is our standard array of tag data
	function refresh($selected='') {
		global $gl;

		$this->model->clear();

		// Stuff the choices in the model
		foreach($this->combo as $choice) {
//		    $this->model->append(array($choice));

		    $this->widget->append_text($choice);
		}

		$this->widget->set_active(0);
		

	}



	/* value of the entered value field */
	function value() {
		$value = $this->dat[$this->selected()]['label'];
		return( $value );
	}


	/* select the given tag and set the value */
/*
	function select($tag,$value=FALSE) {


		// Find the combo index for this tag
		foreach ( $this->combo as $idx => $c_tag ) {
			if ( $tag == $c_tag ) {
				wb_set_selected( $this->ctrl, $idx );					//	select it
				$this->clicked();										// refresh all associated displays
				if ( $value ) {
					wb_set_text( wb_get_control( $this->parent_ctrl, $this->value_ctrl ) , $value );
				}				
			}
		}
	
	}
*/
	function selected() {
		$value = $this->widget->get_active();
		return( $value );
	}


}



class label extends control {

	function __construct($glade,$name) {
		parent::__construct($glade,$name);
		$this->widget->set_label(settings::xlate($this->widget->get_label()));
	}
	
	function set_text($label) {
		$this->widget->set_label(settings::xlate($label));
	}

	function get_text() {
		return($this->widget->get_label());	
	}


	function fg_colour($colour = '#000099') {
		$this->widget->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse($colour)); // note 1 
	}

	function bg_colour($colour = '#ffffff') {
		$this->widget->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colour)); // note 1 
	}

}

/**
*	GtkNotebook
*	
*	@param	
*	@return	
*	
*/
class notebook extends control {

	function __construct($glade,$name) {
		parent::__construct($glade,$name);
	}
	
	function set_page($n) {
		$this->widget->set_current_page($n);
	}

	function get_page() {
		$cur_page = $this->widget->get_current_page();
		return($cur_page);
	}

	
}



/**
*	GtkFrame
*	
*	@param	
*	@return	
*	
*/
class frame extends control {

	function __construct($glade,$name) {
		parent::__construct($glade,$name);
	}
	
	function bg_colour($colour = '#ffffff') {
		$this->widget->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($colour)); // note 1 
	}
	
}


class listview extends control {

	public	$selected_column=FALSE;

	var $col_map = array();				// array( {} => {gtk col number}

	// green
/*
	var $base_bg	= '#BED792';	// alternate row background colours
	var $row1_bg	= '#DBE8C2';	// alternate row background colours
	var $row2_bg	= '#BED792';	// alternate row background colours
*/

	// yellow
/*
	var $base_bg	= '#FFFBE3';	// alternate row background colours
	var $row1_bg	= '#FFF6B8';	// alternate row background colours
	var $row2_bg	= '#FFFBE3';	// alternate row background colours
*/
	// turquoise
/*
	var $base_bg	= '#3DB1D8';	// alternate row background colours
	var $row1_bg	= '#006699';	// alternate row background colours
	var $row2_bg	= '#3DB1D8';	// alternate row background colours
*/

	// Light blue
	var $base_bg	= '#D9E5FB';	// alternate row background colours
	var $row1_bg	= '#EFF4FD';	// alternate row background colours
	var $row2_bg	= '#D9E5FB';	// alternate row background colours

	// Light grey
/*
	var $base_bg	= '#F3F3F3';	// alternate row background colours
	var $row1_bg	= '#D6D6D6';	// alternate row background colours
	var $row2_bg	= '#F3F3F3';	// alternate row background colours
*/

	// All white
/*
	var $base_bg	= '#FFFFFF';	// alternate row background colours
	var $row1_bg	= '#FFFFFF';	// alternate row background colours
	var $row2_bg	= '#FFFFFF';	// alternate row background colours
*/
	var $model=FALSE;
	var $modelsort=FALSE;
	
	var $sort_column = 0;
	var $sort_asc = TRUE;
	
	var $callback = FALSE;			// Caller / parent object for callbacks - popup menu action handlers etc.

	function __construct($glade,$name,$callback=FALSE) {
		parent::__construct($glade,$name,$font);

		// basic background colour
		$this->widget->modify_base(Gtk::STATE_NORMAL, GdkColor::parse($this->base_bg)); // note 1
	
		$this->callback = $callback;

	}

	
	function clear() {
		$this->model->clear();
	}


	function columns() {

	}
	



	/**
	*	Colum heading has been clicked
	*	
	*	@param	
	*	@return	
	*	
	*/
	function on_click_heading($column, $col_no, $col_title) {
		//print "clicked on column: $col_title ($col_no)\n"; // note 2
		// perform your action here

		if ( $this->sort_column == $col_no ) {
			if ( $this->sort_asc ) {
		  		$this->modelsort->set_sort_column_id($col_no, Gtk::SORT_DESCENDING);
				$this->sort_asc = FALSE;
			}
			else {
		  		$this->modelsort->set_sort_column_id($col_no, Gtk::SORT_ASCENDING);
				$this->sort_asc = TRUE;
			}

		}
		else {
			$this->sort_column = $col_no;
	  		$this->modelsort->set_sort_column_id($col_no, Gtk::SORT_ASCENDING);
			$this->sort_asc = TRUE;
		}
		
    	return true;	
	}
	

	/**
	*	Create all columns based upon contents of cols
	*	
	*	@param cols array Array of column setup data
	*	@return	
	*	
	*/
	function render_columns($cols) {
		$col = 0;
		foreach ( $cols as $code => $dat ) {
			$renderer = new GtkCellRendererText();
			
			if ( is_array($dat['edit'] ) ) {

				// Editable cell
				$renderer->set_property('editable', TRUE);
				$renderer->connect("edited", $dat['edit']['edited'], $view, $col);
			}
			else {
				// display only
				$renderer->set_property('editable', FALSE);
			}
			
			
			$column = new GtkTreeViewColumn($dat['heading'], $renderer, 'text', $col);
			$this->widget->append_column($column);
	
	       // setup self-defined function to display alternate row color
	        $column->set_cell_data_func($renderer, array($this,"row_colours")); // note 1 

	 		$column->set_resizable(TRUE);

			$col++;
		}

	
	}



	function refresh() {
		
	}

	function reset() {
		$this->model->clear();
	}
	
	function get_state() {
		$state = array();
		return($state);
	}

	function set_state($state) {
	
	}



	/**
	*	Alternate row colours
	*	
	*	@param	
	*	@return	
	*	
	*/
	function row_colours($column, $cell, $model, $iter) {

		// alternate row colours
	    $path = $model->get_path($iter); // note 2
	    $row_num = $path[0]; // note 2



//$selected = $this->get_selected('id');
//$this_id = $model->get_value($iter, 0);

//echo "[$this_id][$selected]\n";


//		if ( $selected == $row_num ) {
//			$row_color = "#00FF00";
//		}
//		else {
		    $row_color = ($row_num%2==1) ? $this->row1_bg : $this->row2_bg; // note 3
//		}
		
	    $cell->set_property('cell-background', $row_color); // note 4

	}



	/**
	*	Custom sort function for numeric columns
	*	
	*	@param	
	*	@return	
	*	
	*/
	function sort_number($model, $iter1, $iter2) {
	    $item1 = $model->get_value($iter1, $this->sort_column);
	    $item2 = $model->get_value($iter2, $this->sort_column);

	    $val1 = floatval($item1); // note 2
	    $val2 = floatval($item2); // note 2

	    if ($val1<$val2) {
	        return -1; // note 3
	    } elseif ($val1==$val2) {
	        return 0; // note 3
	    } else {
	        return 1; // note 3
	    }	
	}


	function get_selected($column=0) {

		$selection = $this->widget->get_selection();
		list($model, $iter) = $selection->get_selected();
		if ( $iter == NULL ) return(FALSE);

		$val = $model->get_value($iter, $column);
		return( $val );

	}

	function set_sort($column=0,$ascending=TRUE) {

		$this->sort_column = $column;
		$this->sort_asc = $ascending;

		if ( $ascending ) {
	  		$this->modelsort->set_sort_column_id($column, Gtk::SORT_ASCENDING);
		}
		else {
	  		$this->modelsort->set_sort_column_id($column, Gtk::SORT_DESCENDING);
		}

	}



	/**
	*	Set the given $id as the selected row
	*	
	*	@param $file string File name to select.
	*	@return	
	*	
	*/
	function set_selected($id=FALSE,$column=FALSE) {
		if ( $id == FALSE ) return;
		// Iterate the list looking for the given file

		/*
		   1. Get the total number of rows.
		   2. Get the corresponding iter.
		   3. Perform any processing you like here. In this example, we only output the column values of each row.
		*/
		
		$n = $this->modelsort->iter_n_children(NULL); // note 1
	    for ($i=0; $i<$n; ++$i) {
	        $iter = $this->modelsort->get_iter($i); // note 2

	        $value = $this->modelsort->get_value($iter, $column);

			if ( $value == $id ) {

//				$realpath = $this->modelsort->convert_path_to_child_path(array($i));

//print_r($realpath);
//echo "$i : ".$realpath[0]." : $filename : $file\n";

			    $this->widget->scroll_to_cell($i); // note 4
	            $selection = $this->widget->get_selection(); // note 5
	            $selection->select_path($i); // note 6
	            $this->widget->grab_focus();	

	    	}
	    }		
	
	}





}



class pushbutton extends control {

	var $txt = '';

	function __construct($glade,$name) {
		parent::__construct($glade,$name);
	}

	function set_text($txt) {
		$this->widget->set_label($txt);	
	}

	function set_label($txt) {
		$this->widget->set_label($txt);	
	}

	function get_enabled() {

	}


}

/**
*	standard  GtkSpinButton
*	
*	@param	
*	@return	
*	
*/
class spinbutton extends control {

	function __construct($glade,$name) {
		parent::__construct($glade,$name);

	}

	function set_text($txt) {
		$this->widget->set_value(floatval($txt));
	}

	function get_text() {
		$txt = $this->widget->get_value();
		return($txt);
	}


	function set_value($txt) {
		$this->widget->set_value(floatval($txt));
	}

	function get_value() {
		$txt = $this->widget->get_value();
		return($txt);
	}



}



class statusbar extends control {

	function __construct($glade,$name,$font=NULL) {
		parent::__construct($glade,$name,$font);
	}

	function set_text($msg) {
		// display a message
		$context_id = $this->widget->get_context_id('msg1');
		$this->widget->push($context_id, $msg);
	}

}


/**
*	standard  GtkTextView
*	
*	@param	
*	@return	
*	
*/
class textview extends control {

	var $buffer =NULL;

	function __construct($glade,$name,$font=NULL) {
		parent::__construct($glade,$name,$font);

		$this->buffer = new GtkTextBuffer();
		$this->widget->set_buffer($this->buffer);

	}

	function set_text($txt) {
		$this->buffer->set_text($txt);
	}

	function get_text() {
		$txt = $this->buffer->get_text($this->buffer->get_start_iter(), $this->buffer->get_end_iter());
		return($txt);
	}


	function set_editable($v) {
		$this->widget->set_editable($v);
	}

}


/**
*	standard  GtkEntry
*	
*	@param	
*	@return	
*	
*/
class textentry extends control {

	function __construct($glade,$name,$font=NULL) {
		parent::__construct($glade,$name,$font);

	}

	function set_text($txt) {
		$this->widget->set_text($txt);
	}

	function get_text() {
		$txt = $this->widget->get_text();
		return($txt);
	}

}




?>